package com.F04.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.F04.actionForm.F0403ActionForm;
import com.dao.common.DbOperationExt;
import com.util.CommonTools;

public class F0403Dao extends DbOperationExt {

	Logger log = Logger.getLogger(F0401Dao.class);

	public F0403Dao(){
		
	}
	
	public F0403Dao(Connection conn){
		super(conn);
	}
	
	public List select(F0403ActionForm actionForm) throws SQLException{
		List  list = new ArrayList();
		StringBuffer sql = new StringBuffer();
		ResultSet rest = null;
		Map map = null;
		
		String code1 = CommonTools.null2String(actionForm.getFamilyCode1());
		String code2 = CommonTools.null2String(actionForm.getFamilyCode2());
		sql.append("select code,name from pc_material_family where 1=1 ");
		if(!"".equals(code1)){
			sql.append(" and code >= '"+code1+"'");
		}
		if(!"".equals(code2)){
			sql.append(" and code <= '"+code2+"'");
		}
		
		log.info(sql.toString());
		rest = super.executeQuery(sql.toString());
		while(rest.next()){
			map = new HashMap();
			map.put("familyCode", CommonTools.null2String(rest.getString("code")));
			map.put("familyName", CommonTools.null2String(rest.getString("name")));
			list.add(map);
		}
		if(map==null){
			actionForm.setErrorFlag("2");
			return null;
		}
		return list;
	}
	
	public void insert(F0403ActionForm actionForm) throws SQLException{

		StringBuffer sql = new StringBuffer();
		sql.append("insert into pc_material_family(code,name) ");
		sql.append(" values('"+actionForm.getFamilyCode()+"','"+actionForm.getFamilyName()+"')");
		
		log.info(sql.toString());
		super.executeUpdate(sql.toString());
	}
	
	public void selectByCode(F0403ActionForm actionForm) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		ResultSet rest = null;
		
		sql.append("select code,name from pc_material_family");
		sql.append(" where code = '"+CommonTools.null2String(actionForm.getFamilyCode())+"'");
		
		log.info(sql.toString());
		rest = super.executeQuery(sql.toString());
		while(rest.next()){
			actionForm.setFamilyCode(CommonTools.null2String(rest.getString("code")));
			actionForm.setFamilyName(CommonTools.null2String(rest.getString("name")));
		}
	}
	
	public void update(F0403ActionForm actionForm) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		sql.append("update pc_material_family set code = '"+actionForm.getFamilyCode()+"',");
		sql.append("name = '"+actionForm.getFamilyName()+"' ");
		sql.append(" where code = '"+actionForm.getOldCode()+"'");
		
		log.info(sql.toString());
		super.executeUpdate(sql.toString());
	}
	
	public void delete(F0403ActionForm actionForm) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		sql.append("delete from pc_material_family where code = '"+CommonTools.null2String(actionForm.getId())+"'");
		
		log.info(sql.toString());
		super.executeUpdate(sql.toString());
	}
}
